package com.bohua.child.security;

import com.bohua.child.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;

import java.util.Collection;

@Component
public class MyAuthenticationProvider implements AuthenticationProvider {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {

        String username=authentication.getName();//获取账号
        String password= (String) authentication.getCredentials();//获取密码
        User user= (User) userDetailsService.loadUserByUsername(username);
        if(user==null){
            System.out.println("失败");
        }
        Collection<? extends GrantedAuthority> authorities=user.getAuthorities();
        return new UsernamePasswordAuthenticationToken(user,password,authorities);//默认记住用户
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return true;
    }
}
